<?php

sfContext::getInstance()->getConfiguration()->loadHelpers('Picasa');


/**
 * album actions.
 *
 * @package    bolao-aabb
 * @subpackage album
 * @author     Your name here
 */
class albumActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->BolaoAlbums = BolaoAlbumPeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($BolaoAlbum = BolaoAlbumPeer::retrieveByPk($request->getParameter('id')), sprintf('Object BolaoAlbum does not exist (%s).', $request->getParameter('id')));

    $this->BolaoAlbum = $BolaoAlbum;
  }

  public function executeNew(sfWebRequest $request)
  {
    // List available albums on picasaweb
    $all_albums = listAvailableAlbums();

    // Remove already used albums
    $free_albums = array();
    $used_albums = BolaoAlbumPeer::doSelect(new Criteria());
    foreach ($all_albums as $album)
    {
      $found = false;
      foreach ($used_albums as $bolaoAlbum)
      {
        if ($album->getIdnum() == $bolaoAlbum->getPicasaId())
        {
          $found = true;
          break;
        }
      }
      if (!$found) $free_albums[] = $album;
    }

    $this->form = new BolaoAlbumForm($free_albums);
    $this->form['picasa_id']->getWidget()->addOption('formatter', array($this, 'formatter'));
    $this->form['picasa_id']->getWidget()->addOption('album_formatter', array($this, 'album_formatter'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    // Get all available albums
    $albums = listAvailableAlbums();

    $this->form = new BolaoAlbumForm($albums);
    $this->form['picasa_id']->getWidget()->addOption('formatter', array($this, 'formatter'));
    $this->form['picasa_id']->getWidget()->addOption('album_formatter', array($this, 'album_formatter'));

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  /* Edit should not be possible
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($BolaoAlbum = BolaoAlbumPeer::retrieveByPk($request->getParameter('id')), sprintf('Object BolaoAlbum does not exist (%s).', $request->getParameter('id')));
    $this->form = new BolaoAlbumForm($BolaoAlbum);
  }*/

  /* Update should not be possible
  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($BolaoAlbum = BolaoAlbumPeer::retrieveByPk($request->getParameter('id')), sprintf('Object BolaoAlbum does not exist (%s).', $request->getParameter('id')));
    $this->form = new BolaoAlbumForm($BolaoAlbum);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }*/

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($BolaoAlbum = BolaoAlbumPeer::retrieveByPk($request->getParameter('id')), sprintf('Object BolaoAlbum does not exist (%s).', $request->getParameter('id')));
    $BolaoAlbum->delete();

    $this->redirect('album/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $BolaoAlbum = $form->save();

      if ($form['send_email']->getValue())
        $this->redirect('@send_email?type=album&id='.$BolaoAlbum->getId());
      else
        $this->redirect('album/show?id='.$BolaoAlbum->getId());
    }
  }

  public function formatter($widget, $inputs)
  {
    $rows = array();
    foreach ($inputs as $input)
    {
      $rows[] = '<div class="picasa_album">'.$input['input'].'&nbsp;'.$input['label'].'</div>';
    }

    return !$rows ? '' : implode('', $rows);
  }

  public function album_formatter($widget, $album)
  {
    return '<label>'.$album->getTitle().'</label><br/><img src="'.$album->getIcon().'" />';
  }
}
